<template>
  <div class="container">
    <map-cell
      label="所在地"
      placeholder="请选择位置信息"
      required
      :border="false"
      @change="onShow"
    />
    <div>{{showData}}</div>
    <van-button
      type="primary"
      size="large"
      @click="selectlocation"
    >获取值</van-button>
    <h1>当前选择的位置：</h1>
    <div>{{departure}}</div>
  </div>
</template>

<script>
// import Vue from 'vue'

// const chooseLocation = requirePlugin('chooseLocation')
import MapCell from '@/components/form/mapCell'
export default {
  components: {
    MapCell
  },

  data () {
    return {
      departure: '',
      showData: {}
    }
  },
  onLoad (option) {
    // let continent = Object.keys(dist)
    // this.multiArray.push(continent)
    // this.multiArray.push(dist[continent[0]])
  },
  created () {
    console.log('test')
    wx.getLocation({
      // 没有特别说明的都是固定写法
      type: 'wgs84',
      success: res => {
        console.log('location', res)
        // var locationString = res.latitude + ',' + res.longitude
        this.getAddressDetail(res.latitude, res.longitude)
      }
    })
  },

  methods: {
    onShow (data) {
      console.log(data)
      this.showData = data
    },
    getAddressDetail (latitude, longitude) {
      var locationString = latitude + ',' + longitude
      wx.request({
        url: 'https://apis.map.qq.com/ws/geocoder/v1/',
        data: {
          key: 'N75BZ-UTB3K-7GGJW-AVMIL-MDT36-RYFTQ',
          location: locationString
        },
        method: 'GET',
        success: r => {
          // 输出一下位置信息
          console.log(r)
          console.log(r.data.result)
          console.log('用户位置信息', r.data.result.address)
          // r.data.result.address获得的就是用户的位置信息，将它保存到一个全局变量上
          // getApp().globalData.locationInfo = r.data.result.address
          // // 这步是将位置信息保存到本地缓存中，key = value的形式
          // try {
          //   wx.setStorageSync('locationInfo', r.data.result.address)
          // } catch (e) {
          //   console.log(e)
          // }
        }
      })
    },
    selectlocation () {
      // var that = this

      wx.chooseLocation({
        latitude: 36.66645,
        longitude: 117.07641,
        success: res => {
          // success
          console.log(res)
          this.getAddressDetail(res.latitude, res.longitude)
          this.departure = res.name
        },

        fail () {
          // fail
        },

        complete: function () {
          // complete
        }
      })
    }
  }
}
</script>

